<!-- 普通新增跟台账新增 -->
<template>
  <basic-dialog v-model="visible" hideFooter :title="title" width="1000px">
    <avue-form v-model="formData" ref="formRef" :option="option" @submit="handleSubmit"></avue-form>
  </basic-dialog>
</template>

<script setup>
import { computed } from 'vue'
import { apiAddAndBaseInfo, apiAdd } from '@/apis/device/deviceAccount.js'
import { ElMessage } from 'element-plus'
const props = defineProps({
  refresh: Function,
  apiListParams: {
    type: Object,
    default: () => ({})
  }
})
const title = computed(() => {
  if (type.value === 1) {
    return '新增'
  } else {
    return '新增设备台账和基本信息'
  }
})
const visible = ref(false)
const formData = ref()
const formRef = ref()
//  1为普通新增，2为基础信息新增
const type = ref(1)

const disabled = computed(() => {
  return type.value === 1
})
const option = ref({
  labelWidth: 120,
  group: [
    {
      label: '设备的基本信息',
      column: [
        {
          label: '设备名称',
          prop: 'deviceName',
          component: 'BasicCrudTableSelect',
          // 内置参照名称
          dataType: 'device',
          width: 150,
          crudProps: {
            option: {
              column: [
                {
                  prop: 'name',
                  label: '名称',
                  search: true,
                  sortable: true
                }, {
                  label: "设备类型",
                  prop: "deviceCategory",
                  search: true,
                  sortable: true,
                  type: 'select',
                  searchType: 'select',
                  dicData: [{
                    label: '非标',
                    value: 'unStand'
                  }, {
                    label: '通用',
                    value: 'general'
                  }],
                  width: 150,
                }, {
                  prop: 'specification',
                  label: '型号/规格',
                  search: true,
                  sortable: true
                }, {
                  label: "精度/参数",
                  prop: "params",
                  sortable: true,
                  width: 150,
                },
                {
                  label: "制造/供应商",
                  prop: "supplier",
                  sortable: true,
                  width: 150,
                },
                {
                  label: "定检类型",
                  prop: "lifeCountType",
                  sortable: true,
                  propType: "dict",
                  type: "select",
                  hide: true,
                  display: false,
                  dicData: [{
                    label: '使用次数',
                    value: 'checkByCount'
                  }, {
                    label: '使用时间',
                    value: 'checkByTime'
                  }],
                  rules: [{ required: true, message: "寿命计算方式不能为空" }],
                  formatter: (row) => {
                    return row.lifeCountTypeText;
                  },
                  width: 150,
                },
                {
                  label: "密级",
                  prop: "secret",
                  sortable: true,
                  width: 150,
                  type: 'select',
                  dicData: [{
                    label: '非密',
                    value: 10
                  }, {
                    label: '秘密',
                    value: 20
                  }, {
                    label: '机密',
                    value: 30
                  }]
                }
              ]
            }
          },
          comProps: {
            comType: 'form',
            saveFieldMap: {
              deviceName: 'name',
              brand: "brand",
              deviceName: "name",
              material: "material",
              applyProduct: "applyProduct",
              supplier: "supplier",
              priceOfUnit: "priceOfUnit",
              materialLife: "materialLife",
              deviceCategory: 'deviceCategory',
              specification: 'specification',
              lifeCountType: 'lifeCountType',
              secret: 'secret',
              params: 'params'
            },
          },
          curForm: formData,
          change: (newV) => {
            formData.value = newV
            formRef.value.clearValidate()
          },
          rules: [{ required: true, message: "设备名称不能为空" }],
        },
        {
          label: "设备类型",
          prop: "deviceCategory",
          search: true,
          sortable: true,
          type: 'select',
          searchType: 'select',
          dicData: [{
            label: '非标',
            value: 'unStand'
          }, {
            label: '通用',
            value: 'general'
          }],
          width: 150,
          disabled: disabled
        },

        {
          label: "型号/规格",
          prop: "specification",
          disabled: disabled
        },
        {
          label: "精度/参数",
          prop: "params",
          sortable: true,
          width: 150,
          disabled: disabled
        },
        {
          label: "定检计算类型",
          prop: "lifeCountType",
          sortable: true,
          propType: "select",
          type: "select",
          display: false,
          dicData: [{
            label: '使用次数',
            value: 'checkByCount'
          }, {
            label: '使用时间',
            value: 'checkByTime'
          }],
          width: 150,
          hide: true,
          disabled: disabled,
          rules: [{ required: true, message: "定检计算类型不能为空" }],
        },
        {
          label: "密级",
          prop: "secret",
          sortable: true,
          width: 150,
          type: 'select',
          disabled: disabled,
          dicData: [{
            label: '非密',
            value: '10'
          }, {
            label: '秘密',
            value: '20'
          }, {
            label: '机密',
            value: '30'
          }]
        },
        {
          label: "制造/供应商",
          prop: "supplier",
          disabled: disabled,
        },
        {
          label: "品牌",
          disabled: disabled,
          prop: "brand",
        },
        {
          disabled: disabled,
          label: "材质",
          prop: "material",
        },
        {
          label: "适用产品",
          disabled: disabled,
          prop: "applyProduct",
        },

        {
          label: "单价",
          type: "number",
          prop: "priceOfUnit",
          precision: 2,
          rules: [
            { type: "number", message: "单价必须为数字值", trigger: "blur" },
          ],
          disabled: disabled,
        },
        {
          label: "寿命",
          prop: "materialLife",
          disabled: disabled,
        },
      ]
    },
    {
      label: '实物信息',
      column: [
        {
          label: "设备编号",
          prop: "code",
          rules: [{ required: true, message: "设备编号不能为空" }],
        },
        {
          label: "所属库房",
          prop: "warehouseName",
          fieldKey: 'warehouse',
          referType: 'warehouse',
          component: 'BasicCrudInputTree',
          saveFieldMap: { warehouseName: 'name', warehouse: 'id' },
          curForm: formData,
          change: (val) => {
            formData.value = val
          },
          rules: [{ required: true, message: "所属库房不能为空" }],
        },
        {
          label: "购置时间",
          prop: "buyDate",
          type: "date",
        },
        {
          label: "设备位置",
          prop: "fixedAddress",
        },
        {
          label: "备注",
          prop: "des",
        },
        {
          label: "存放货位",
          prop: "goodsLocationName",
          fieldKey: 'goodsLocation',
          referType: 'goodsLocation',
          component: 'BasicCrudInputTree',
          saveFieldMap: { goodsLocationName: 'name', goodsLocation: 'id' },
          curForm: formData,
          autoLoad: false,
          paramsFielsMap: { warehouse: 'warehouse' },
          change: (val) => {
            formData.value = val
          },
        },
        {
          label: "当前使用次数",
          type: "number",
          prop: "currentUsedCount",
          value: 0,
        },
      ]
    }, {
      label: '保养内容',
      column: [
        {
          label: "有效期",
          prop: "effectiveDate",
          type: "date",
          rules: [{ required: true, message: "有效期不能为空" }],
        }, {
          label: '负责人',
          prop: 'deviceAdminName',
          component: 'BasicCrudTableSelect',
          prop: 'deviceAdminName',
          // 内置参照名称
          dataType: 'user',
          width: 150,
          comProps: {
            comType: 'form',
            saveFieldMap: { deviceAdminName: 'name', deviceAdmin: 'id' },
          },
          curForm: formData,
          change: (newV) => {
            formData.value = newV
          },
        },
        {
          label: "保养周期",
          prop: "inspectionCycle",
          type: "number",
          min: 1,
          precision: 0,
          value: 3,
          rules: [{ required: true, type: 'number', message: "保养周期只能是数字" }],
        },
        {
          label: "保养周期类型",
          prop: "inspectionDateType",
          type: "select",
          dicData: [{
            label: '月',
            value: 'month'
          }, {
            label: '年',
            value: 'year'
          }],
          value: 'month'
        },
        {
          label: "上次保养时间",
          prop: "inspectionDate",
          type: "date",
          format: 'yyyy-MM-dd',
          disabled: true
        },
        {
          label: "上次保养人",
          prop: "inspectionUserName",
          formatter: (val) => {
            return val.inspectionUserCode + " " + val.inspectionUserName;
          },
          disabled: true
        }
      ]
    }
  ]
})
const open = (val) => {
  type.value = val
  visible.value = true
}

const close = () => {
  formRef.value.resetFields()
  visible.value = false
}
// 提交
const handleSubmit = (form, done) => {
  const apiFun = type.value === 1 ? apiAdd : apiAddAndBaseInfo
  apiFun({
    ...form,
    ...props.apiListParams
  }).then(() => {
    ElMessage.success('新增成功！')
    close()
    props.refresh && props.refresh()

  }).finally(() => {
    done()
  });
}
defineExpose({
  open
})
</script>

<style lang='scss' scoped></style>